﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using HdipCricketClub.Models;

namespace HdipCricketClub.Controllers
{ 
    public class StatisticsController : Controller
    {
        private CricketClubEntities db = new CricketClubEntities();

        //
        // GET: /Statistics/

        public ViewResult Index()
        {
            return View();
        }

        //
        //  GET:  /Statistics/SeasonStatistics/

        public ActionResult SeasonStatistics(string sortOrder, string searchString)
        {
            ViewBag.LastNameSortParam = String.IsNullOrEmpty(sortOrder);
            
            var seasonstatistics = db.PlayerSeasonStatistics.Include("Member").OrderBy(m => m.Member.last_name);

            //if (!String.IsNullOrEmpty(searchString))
            //{
            //    seasonstatistics = seasonstatistics.Where(m => m.Member.last_name.ToUpper().Contains(searchString.ToUpper()));
            //}
            
            return View(seasonstatistics.ToList());
        }
        
        //
        //  GET: /Statistics/BattingAverages/

        public ActionResult BattingAverages(string sortOrder, string searchString)
        {
            ViewBag.LastNameSortParam = String.IsNullOrEmpty(sortOrder);

            var seasonstatistics = db.PlayerSeasonStatistics.Include("Member")
                                                      .Where(ss => ss.innings >= 1);

            if (!String.IsNullOrEmpty(searchString))
            {
                var seasonstatisticsquery = db.PlayerSeasonStatistics.Include("Member").Select(ss => ss);

                seasonstatistics = seasonstatisticsquery.Where(m => m.Member.last_name.ToUpper().Contains(searchString.ToUpper()));
            }
            
            return View(seasonstatistics.ToList());
        }
        
        //
        //  GET:    /Statistics/BowlingAverages/

        public ActionResult BowlingAverages(string sortOrder, string searchString)
        {
            ViewBag.LastNameSortParam = String.IsNullOrEmpty(sortOrder);

            var seasonstatistics = db.PlayerSeasonStatistics.Include("Member").Select(ss => ss)
                                                      .Where(ss => ss.overs > 0);

            if (!String.IsNullOrEmpty(searchString))
            {
                seasonstatistics = seasonstatistics.Where(m => m.Member.last_name.ToUpper().Contains(searchString.ToUpper()));
            }

            return View(seasonstatistics.ToList());
        }
        
        //
        //  GET:    Statistics/CareerStatistics/

        public ActionResult CareerStatistics()
        {
            return View();
        }
        
        //
        // GET: /Statistics/Details/5

        public ViewResult Details(int id)
        {
            var seasonstatistic = db.PlayerSeasonStatistics.Single(s => s.player_id == id);
            return View(seasonstatistic);
        }

        //
        // GET: /Statistics/Create

        public ActionResult Create()
        {
            ViewBag.player_id = new SelectList(db.Members, "member_id", "first_name");
            return View();
        } 

        //
        // POST: /Statistics/Create

        [HttpPost]
        public ActionResult Create(PlayerSeasonStatistic seasonstatistic)
        {
            if (ModelState.IsValid)
            {
                db.PlayerSeasonStatistics.AddObject(seasonstatistic);
                db.SaveChanges();
                return RedirectToAction("Index");  
            }

            ViewBag.player_id = new SelectList(db.Members, "member_id", "first_name", seasonstatistic.player_id);
            return View(seasonstatistic);
        }
        
        //
        // GET: /Statistics/Edit/5
 
        public ActionResult Edit(int id)
        {
            PlayerSeasonStatistic seasonstatistic = db.PlayerSeasonStatistics.Single(s => s.player_id == id);
            ViewBag.player_id = new SelectList(db.Members, "member_id", "first_name", seasonstatistic.player_id);
            return View(seasonstatistic);
        }

        //
        // POST: /Statistics/Edit/5

        [HttpPost]
        public ActionResult Edit(PlayerSeasonStatistic seasonstatistic)
        {
            if (ModelState.IsValid)
            {
                db.PlayerSeasonStatistics.Attach(seasonstatistic);
                db.ObjectStateManager.ChangeObjectState(seasonstatistic, EntityState.Modified);
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            ViewBag.player_id = new SelectList(db.Members, "member_id", "first_name", seasonstatistic.player_id);
            return View(seasonstatistic);
        }

        //
        // GET: /Statistics/Delete/5
 
        public ActionResult Delete(int id)
        {
            PlayerSeasonStatistic seasonstatistic = db.PlayerSeasonStatistics.Single(s => s.player_id == id);
            return View(seasonstatistic);
        }

        //
        // POST: /Statistics/Delete/5

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(int id)
        {            
            PlayerSeasonStatistic seasonstatistic = db.PlayerSeasonStatistics.Single(s => s.player_id == id);
            db.PlayerSeasonStatistics.DeleteObject(seasonstatistic);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}